﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-12-29 19:04
    /// 描 述：CourseRegistration服务类
    /// </summary>
    public class CourseRegistrationService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<CourseRegistrationEntity>> GetList(CourseRegistrationListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<CourseRegistrationInfo>> GetPageList(CourseRegistrationListParam param, Pagination pagination)
        {
            var strSql = new StringBuilder();
            List<DbParameter> filter = new List<DbParameter>();
            
            strSql.Append(@"SELECT 
                            a.Id,
                            a.user_id,
                            a.course_id,
                            a.order_id,
                            a.name,
                            a.gender,
                            a.height,
                            a.age,
                            a.weight,
                            a.phone,
                            a.status,
                            a.fee,
                            a.payment_time,
                            a.remark,
                            a.create_time,
                            b.title as CourseName,
                            c.nickname as UserName,
                            c.telephone as UserPhone
                        FROM course_registration a
                        LEFT JOIN courses b ON a.course_id = b.Id
                        LEFT JOIN customer_user c ON a.user_id = c.Id
                        WHERE a.deleted = 0");

            if (!string.IsNullOrEmpty(param.UserName))
            {
                strSql.Append(" AND (c.nickname like @UserName)");
                filter.Add(DbParameterExtension.CreateDbParameter("@UserName", '%' + param.UserName + '%'));
            }
            if (!string.IsNullOrEmpty(param.Phone))
            {
                strSql.Append(" AND (a.phone like @Phone OR c.telephone like @Phone)");
                filter.Add(DbParameterExtension.CreateDbParameter("@Phone", '%' + param.Phone + '%'));
            }
            if (!string.IsNullOrEmpty(param.CourseName))
            {
                strSql.Append(" AND b.title like @CourseName");
                filter.Add(DbParameterExtension.CreateDbParameter("@CourseName", '%' + param.CourseName + '%'));
            }
            if (param.Status.HasValue)
            {
                strSql.Append(" AND a.status = @Status");
                filter.Add(DbParameterExtension.CreateDbParameter("@Status", param.Status.Value));
            }

            var list = await this.BaseRepository().FindList<CourseRegistrationInfo>(strSql.ToString(), filter.ToArray(), pagination);
            return list.ToList();
        }

        public async Task<CourseRegistrationEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<CourseRegistrationEntity>(id);
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(CourseRegistrationEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                
                await this.BaseRepository().Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<CourseRegistrationEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<CourseRegistrationEntity, bool>> ListFilter(CourseRegistrationListParam param)
        {
            var expression = LinqExtensions.True<CourseRegistrationEntity>();
            if (param != null)
            {
            }
            return expression;
        }
        #endregion
    }
}
